import request from "@/utils/service/request";
let allApi = {};
/**
 * 处理api为具体请求方法
 *  @param {Array}  apiList  - 接口列表
 * */
export function dealApiList(apiList) {
    apiList.forEach((item) => {
        switch (item.type) {
            // post提交请求
            case "post":
                allApi[item.name] = (params = {}, requestConfig) => {
                    return request(requestConfig)({
                        url: item.url,
                        method: "post",
                        data: JSON.stringify({
                            ...params,
                        }),
                        headers: {
                            "content-type": "application/json",
                        },
                    });
                };
                break;
            // put请求更新
            case "put":
                allApi[item.name] = (params = {}, requestConfig) => {
                    return request(requestConfig)({
                        url: item.url,
                        method: "put",
                        data: JSON.stringify({
                            ...params,
                        }),
                        headers: {
                            "content-type": "application/json",
                        },
                    });
                };
                break;
            // delete请求删除
            case "del":
                allApi[item.name] = (params = {}, requestConfig) => {
                    return request(requestConfig)({
                        url: item.url,
                        method: "delete",
                        data: JSON.stringify({
                            ...params,
                        }),
                        headers: {
                            "content-type": "application/json",
                        },
                    });
                };
                break;
            // post方式请求文件流
            case "formDataPost":
                allApi[item.name] = (params, requestConfig) => {
                    return request(requestConfig)({
                        url: item.url,
                        method: "post",
                        data: params,
                        headers: {
                            "Content-type": "multipart/form-data",
                        },
                    });
                };
                break;
            // 默认get请求
            default:
                allApi[item.name] = (params = {}, requestConfig) => {
                    return request(requestConfig).get(`${item.url}`, {
                        params,
                    });
                };
        }
    });

    return allApi;
}
